<template>
  <!--模态窗 单行-->
  <transition
    name="custom-classes-transition"
    enter-active-class="animated fadeInDown"
    leave-active-class="animated fadeOutUp"
  >

    <div class="modal-ctrl-body" v-show="RevampModalObj.isShow">
      <div class="modal-ctrl-main odd-row-ctrl">
        <div class="modal-title">
          <p>新增手台</p>
        </div>
        <div class="modal-ctrl-content">
          <div class="modal-row">
            <div class="modal-form-key f-l indispensable">
              手台编号：
            </div>
            <input type="text" readonly="readonly" maxlength="16" placeholder="最长16个字符" v-model="radioData.NUM" class="modal-text f-l">
          </div>
          <div class="modal-row">
            <div class="modal-form-key f-l indispensable">
              所属单位：
            </div>
            <input type="text" v-model="stationName" placeholder="请选择所属单位" @click="getStationTree" readonly="readonly" class="modal-text f-l">
            <input v-model="radioData.STATIO_ID" type="hidden">
            <div class="sectionTree" v-show="sectionTreeIShow">
              <div id="sectionTreeRevamp" class="ztree sectionTree-box"></div>
            </div>
          </div>
          <div class="modal-row">
            <div class="modal-form-key f-l">
              手台类型：
            </div>
            <select @change="selectType" :value="radioData.TYPE">
              <option value="0">手台</option>
              <option value="1">车台</option>
              <option value="2">基地台</option>
            </select>
          </div>
          <div class="modal-row">
            <div class="modal-form-key f-l">
              在线状态：
            </div>
            <select @change="selectState" :value="radioData.STATE">
              <option value="1">在线</option>
              <option value="2">离线</option>
            </select>
          </div>
          <div class="modal-row">
            <div class="modal-form-key f-l">
              工作状态：
            </div>
            <select @change="selectIsWork" :value="radioData.IS_WORK">
              <option value="1">正常</option>
              <option value="2">损坏</option>
            </select>
          </div>
          <div class="modal-row">
            <div class="modal-form-key f-l">
              逻辑删除：
            </div>
            <select @change="selectIsDelete" :value="radioData.IS_DELETE">
              <option value="0">不删除</option>
              <option value="1">删除</option>
            </select>
          </div>
        </div>
        <div class="btns text-r">
          <button type="button" class="btn btn-default" @click="close">取消</button>
          <button type="button" class="btn btn-info" @click="subBtn">确定</button>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
  export default {
    name: "RevampRadio",
    props:['RevampModalObj','selectRadioId'],
    data(){
      return{
        radioData:{
          NUM:'',
          STATIO_ID:'',
          TYPE:'0',
          STATE:'1',
          IS_WORK:'1',
          IS_DELETE:'0'
        },
        stationName:'',
        setting : {
          data: {
            simpleData: {
              enable: true
            },
          }
        },
        sectionTreeIShow:false,
        radioId:""
      }
    },
    watch:{
      RevampModalObj:{
        handler(v1){
          if( v1.isShow ){
            this.radioId = this.selectRadioId;
            //获取详情
            this.getResourceData()
          }
        },
        deep:true
      }
    },
    mounted(){
      let _this = this;
      this.setting.callback = {
        onClick:function(event,treeId,treeNode){ //新增警员部门树选择
          //console.log(treeNode);
          _this.radioData.STATIO_ID = treeNode.id;
          _this.stationName = treeNode.name;
          _this.sectionTreeIShow = !_this.sectionTreeIShow;
        }
      };
      this.$axios.get(this.HOST+'/station/getOrgaInfo')
        .then((data)=>{
          $.fn.zTree.init($("#sectionTreeRevamp"), this.setting, data.data.data);
          //console.log(data);
        })
        .catch((err)=>{
          //console.log(err);
        })
    },
    methods:{
      //提交
      subBtn(){
        //console.log(this.radioData);

        let searchParams = {
          "columns":["*"],
          "conditions":[
            {
              "field": "ID",
              "symbol": "!=",
              "value": this.radioId
            },
            {"field": "NUM","symbol":"=","value":this.radioData.NUM}
          ],
          "dataTypeCode":"policefacility",
          "pageIndex":1,
          "pageSize":20,
          "tableName":"T_RES_RADIO"
        };

        this.$axios.post(this.HOST+"/base/query/page",searchParams)
          .then((data)=>{
            if( data.data.data.list.length > 0 ){
              this.s_alert("已存在该手台编号！");
            }else{
              if( this.verifyForm.isNull(this.radioData.NUM) ){
                this.s_alert("手台编号为空或格式不正确！");
                return false;
              }else if( this.verifyForm.isNull(this.radioData.STATIO_ID) ){
                this.s_alert("所属单位不能为空！");
                return false;
              }

              let paramsKey = [];
              let paramsValue = [];

              for( let key in this.radioData ){
                paramsKey.push(key);
                paramsValue.push(this.radioData[key]);
              }

              let params = {
                "conditions": [
                  {
                    "field": "ID",
                    "symbol": "=",
                    "value": this.radioId
                  }
                ],
                "data": this.radioData,
                "dataTypeCode": "policefacility",
                "tableName": "T_RES_RADIO"
              };
              //console.log(params);
              //PUT /base/modify
              this.$axios.put(this.HOST+"/base/modify",params)
                .then((data)=>{
                  //console.log(data);
                  if( data.data.success ){
                    this.s_alert("修改成功！");
                    this.s_logwrite("资源信息管理-手台管理","修改手台");
                    this.close()
                  }else{
                    this.s_alert("修改失败！")
                  }
                })
                .catch((err)=>{
                  //console.log(err);
                })
            }
          })
          .catch(err=>{
            //console.log(err);
          });
      },
      //获取详细数据
      getResourceData(){
        //console.log(this.radioId);
        let params = {
          "columns":["*"],
          "conditions": [
          {
            "field": "ID",
            "symbol": "=",
            "value": this.radioId
          }
        ],
          "dataTypeCode": "policefacility",
          "tableName": "T_RES_RADIO"
        };
        this.$axios.post(this.HOST+"/base/query/page",params)
          .then((data)=>{
            //console.log(data);
            let successData = data.data.data.list[0];
            for( let radioDataKey in this.radioData ){
              for( let successKey in successData ){
                if( radioDataKey === successKey ){
                  this.radioData[radioDataKey] =  successData[successKey];
                }
              }
            }
            this.stationName = successData.STATIO_ID_PARSE;
            //console.log(this.radioData);
          })
          .catch((err)=>{
            //console.log(err);
          })
      },
      //电台选择
      selectType(ele){
        ////console.log(ele.target.value);
        this.radioData.TYPE = ele.target.value
      },
      //在线状态
      selectState(ele){
        ////console.log(ele.target.value);
        this.radioData.STATE = ele.target.value
      },
      //工作状态
      selectIsWork(ele){
        ////console.log(ele.target.value);
        this.radioData.IS_WORK = ele.target.value
      },
      //逻辑删除
      selectIsDelete(ele){
        ////console.log(ele.target.value);
        this.radioData.IS_DELETE = ele.target.value
      },
      //展开部门树
      getStationTree(){
        this.sectionTreeIShow = !this.sectionTreeIShow;
      },
      //关闭弹窗
      close(){
        this.$emit("RevampRadioModalClose");
        this.clearData(); //关闭弹窗清除数据
      },
      //清除数据
      clearData(){
        this.radioData={
          NUM:'',
          STATIO_ID:'',
          TYPE:'0',
          STATE:'1',
          IS_WORK:'1',
          IS_DELETE:'0'
        };
        this.stationName='';
        this.sectionTreeIShow = false;
      }
    }
  }
</script>

<style scoped>
  .sectionTree{
    position: absolute;
    top: 30px;
    right: 0;
    width: 80%;
    z-index: 2;
    background-color: #fff;
    border:1px solid rgba(51,51,51,0.2);
    box-sizing: border-box;
    padding: 5px;
    max-height: 200px;
    overflow-y: auto;
  }
  .fadeInDown,.fadeOutUp{
    animation-duration: .500s;
  }
</style>
